<?php

namespace App\Admin\Controllers\Financial;

use App\Admin\Models\Financial\FinancialRate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Admin\Validate\Financial\FinancialRate as ValidateFinancialFinancialRate;

use App\Common\Services\CommonExcelExport;
use Maatwebsite\Excel\Facades\Excel;
class FinancialRateController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = FinancialRate::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = FinancialRate::where(['id' => $id,'is_del'=>1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['pay_type_id', 'money_start', 'money_end', 'val_type', 'regular_value', 'rate_value','remark']);

        $Validate = new ValidateFinancialFinancialRate;
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        if ($req_data['val_type'] == 1) {
            if(!isset($req_data['regular_value']) || $req_data['regular_value']<0){
                return response()->json(['code' => '001', 'msg' => '固定值不能为空']);
            }
            $req_data['rate_value']=0;
        } else {
            if(!isset($req_data['rate_value']) || $req_data['rate_value']<0){
                return response()->json(['code' => '001', 'msg' => '费率不能为空']);
            }
            $req_data['regular_value']=0;
        }



        $data = $req_data;
        $data['create_at'] = time();
        $data['update_at'] = time();
        FinancialRate::create($data);
        return response()->json(['code' => '000', 'msg' => '新增费率成功']);
    }

    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id','pay_type_id', 'money_start', 'money_end', 'val_type', 'regular_value', 'rate_value','remark']);

        $Validate = new ValidateFinancialFinancialRate;
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        if ($req_data['val_type'] == 1) {
            if(!isset($req_data['regular_value']) || $req_data['regular_value']<0){
                return response()->json(['code' => '001', 'msg' => '固定值不能为空']);
            }
            $req_data['rate_value']=0;
        } else {
            if(!isset($req_data['rate_value']) || $req_data['rate_value']<0){
                return response()->json(['code' => '001', 'msg' => '费率不能为空']);
            }
            $req_data['regular_value']=0;
        }

        $info = FinancialRate::where(['id'=>$req_data['id'],'is_del'=>1])->first();
        if(!$info){
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }



        $data = $req_data;
        $data['update_at'] = time();
        FinancialRate::where(['id'=>$req_data['id'],'is_del'=>1])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑费率成功']);
    }

    public function dopostdel(Request $request)
    {
        $id=$request->id ?? 0;
        $info = FinancialRate::where(['id'=>$id,'is_del'=>1])->first();
        if(!$info){
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        FinancialRate::where(['id'=>$id])->update(['is_del'=>2,'del_at'=>time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    public function doexport(Request $request)
    {
        $list = FinancialRate::getList($request)['list'];
        $exceldata = [];
        $row = [
            ['id'=>'费率表'],
            [
                'id' => '序号',
                'paytype_name' => '支付方式',
                'money_start' => '开始金额',
                'money_end' => '结束金额',
                'val_type' => '费率类型',
                'regular_value' => '固定值',
                'rate_value' => '费率',
            ]
        ];
        foreach ($list as $key=>$val) {
            $exceldata[] = [
                
                'id' => $key+1,
                'paytype_name' => $val['paytypeinfo']['paytype_name'],
                'money_start' => $val['money_start']/100,
                'money_end' => $val['money_end']/100,
                'val_type' => $val['val_type']==1?'固定值':'费率',
                'regular_value' => $val['regular_value']/100,
                'rate_value' => $val['rate_value'],
            ];
        }

        $filename = '费率信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}